package com.lzh.lzhframework.handler;

import com.lzh.lzhframework.domain.ResponseResult;
import com.lzh.lzhframework.exception.SystemException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 全局异常处理
 *
 * @author lzh
 */
@Slf4j
@ControllerAdvice(annotations = {RestController.class})
@ResponseBody
public class GlobalExceptionHandler {

    /**
     * 异常处理方法
     *
     * @return
     */
    @ExceptionHandler(SystemException.class)
    public ResponseResult exceptionHandler(SystemException ex) {
        return ResponseResult.errorResult(ex.getCode(), ex.getMessage());
    }

    /**
     * 校验异常捕获
     *
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseResult exceptionHandler(MethodArgumentNotValidException e) {
        return ResponseResult.errorResult(600, e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
    }
}